#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int fla=0;
static int callback(void *data, int argc, char **argv, char **azColName){
    int i;
    // fprintf(stderr, "%s: ", (const char*)data);
    if(fla==0){
        for(i=0; i<argc; i++){
            printf("%-5s\t", azColName[i]);
            fla=1;
        }
        printf("\n");}
    for(i=0; i<argc; i++){
        printf("%-5s\t", argv[i] ? argv[i] : "NULL");
    }
    // fprintf(stdout, "data: %s: argc:%d azColName:%s  argv[%s]\n",
    //         (const char*)data,argc,*azColName,*argv);
    // for(i=0; i<argc; i++){
    //     fprintf(stdout, "data: %s: argc:%d azColName:%s  argv[%s]\n",
    //             (const char*)data,argc,azColName[i],argv[i]);
    //     printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    // }
    printf("\n");
    return 0;
}

int main(int argc, char* argv[])
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    const char* data = "Callback function called";

    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }else{
        fprintf(stderr, "Opened database successfully\n");
    }

    /* Create SQL statement */
    sql = "SELECT * from COMPANY";

    /* Execute SQL statement */
    char *errMsg;
    char **dbResult;
    int nRow = 0, nColumn = 0;
    int result;
    sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&errMsg);
    for (int i = 0; i <= nRow; i++)
    {
        for (int j = 0; j < nColumn; j++)
        {
            printf("%-5s\t",dbResult[i*nColumn+j]);
        }
        printf("\n");
    }
    

    sqlite3_close(db);
    return 0;
}